<?php
// 密码加密
function passEncryp($password)
{
    return md5(md5($password).md5('蓝月'));
}

// 取随机字符串
function getRandCode($num = 40)
{
    $array = array('A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    $tmpstr = '';
    $max = count($array);
    for ($i = 1; $i <= $num; $i++) {
        $key = rand(0, $max - 1);
        $tmpstr .= $array[$key];
    }
    return $tmpstr;
}

//邮箱格式验证
function validataIsEmail($email)
{
    $preg_email = '/^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@([a-zA-Z0-9]+[-.])+([a-z]{2,5})$/ims';
    if (preg_match($preg_email, $email)) {
        return true;
    }
    return false;
}


/**
 * [returnJson 返回值封装]
 * @param  [type] $code [错误代码]
 * @param  [type] $msg  [信息]
 * @param  array $data [description]
 * @return [type]       [description]
 */
function returnJson($code = '', $msg = '', $data = [])
{
    $returnArr['code'] = $code;
    $returnArr['msg'] = $msg;
    $returnArr['data'] = (object)$data;
    return response()->json($returnArr);
}

/**
 * 生成唯一的订单号
 * @param $str
 * @return string
 */
function getOrderSn($str)
{
    return $str . date('YmdHis', time()) . uniqid();
}

/**
 * 隐藏字符串信息
 * @param $str
 * @return mixed
 */
function hideStr($str)
{
    $len = strlen($str) - 2 > 0 ? strlen($str) - 2 : 0;

    $reStr = '';
    for ($i = 0; $i < $len; $i++) {
        $reStr .= '*';
    }
    return substr_replace($str, $reStr, 1, $len);
}


// post 请求
function curlPost($url, $post = []) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post) );
    $res = curl_exec($ch);
    curl_close($ch);
    return json_decode($res,true);
}